package com.yjxxt.DBUtil;

import java.sql.*;

/**
 * packageName com.yjxxt.DBUtil
 *
 * @author TongHao
 * @date 2025/8/4
 * @description 数据库连接类
 */
public class DBUtil {

    public static final String Driver = "com.mysql.cj.jdbc.Driver";
    public static final String URL = "jdbc:mysql://localhost:3306/mvc0721?serverTimezone=UTC&useServerPrepStmts=true";
    public static final String USER = "root";
    public static final String PASSWORD = "123456";

    static Connection conn = null;

    static PreparedStatement ps = null;

    static ResultSet rs = null;

    static ResultSetMetaData md = null;
    public static Connection getConn() {
        try {
            //1、加载驱动类
            Class.forName(Driver);
            //2、获取数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }


    public static void close(ResultSet rs, PreparedStatement ps,Connection conn) {
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (conn!=null){
                conn.close();
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
